package com.bytesizedhosting.api.android.darthcrap.components.accordian;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bytesizedhosting.api.android.darthcrap.R;

/*
 * Changes:
 * * Do not use AccordianFactory. Instead create an accordian directly.
 * * Allow for a parameter of Accordian.State to be passed to the accordian constructor.
 * * holdState saves it's state (hashcode+openstates map, scroll position) into the state, only if one is provided
 * * restoreState tries to read the state if there is one. It will keep items open in position order if their hashcodes match. It will scroll to position if there are the same amount of items with the same hashcodes.
 */

public abstract class AccordianEntry implements OnClickListener {
	private ViewGroup objRoot = null;
	private boolean boolIsOpen = false;
	private String strTitle = "";
	private boolean boolShowExclamation = false;
	
	public AccordianEntry() {
		this(null,false);
	}
	
	public AccordianEntry(String strTitle) {
		this(strTitle,false);
	}
	
	public AccordianEntry(boolean boolShowExclamation) {
		this(null,boolShowExclamation);
	}
	
	public AccordianEntry(String strTitle, boolean boolShowExclamation) {
		setTitle(strTitle);
		setExclamationState(boolShowExclamation);
	}
	
	
	protected void setTitle(String strTitle) {
		if (strTitle == null) {
			strTitle = "";
		}
		this.strTitle = strTitle;
		if (this.objRoot != null) {
			((TextView)this.objRoot.findViewById(R.id.lblNewsItemTitle)).setText(this.strTitle);
			((TextView)this.objRoot.findViewById(R.id.lblNewsItemHeader)).setText(this.strTitle);
		}
	}
	
	protected void setExclamationState(boolean boolShowExclamation) {
		this.boolShowExclamation = boolShowExclamation;
		if (this.objRoot != null) {
			this.objRoot.findViewById(R.id.imgNewsItemWarning).setVisibility((this.boolShowExclamation)?View.VISIBLE:View.GONE);
		}
	}
	
	void buildEntry(LayoutInflater objInflater, LinearLayout objRootLayout, boolean boolIsFirstEntry) {
		this.objRoot = (ViewGroup)objInflater.inflate(R.layout.accordianitem, objRootLayout, false);
		objRootLayout.addView(this.objRoot);

		setTitle(this.strTitle);
		setExclamationState(this.boolShowExclamation);
		
    	setStateHandler(R.id.lblNewsItemTitle);
    	setStateHandler(R.id.imgNewsItemArrow);
    	setStateHandler(R.id.imgNewsItemBackground);
    	setStateHandler(R.id.imgNewsItemWarning);
    	
    	if (boolIsFirstEntry) {
    		((ViewGroup.MarginLayoutParams)this.objRoot.getLayoutParams()).topMargin = 0;
    	}
		
		buildEntryView(objInflater,(LinearLayout) this.objRoot.findViewById(R.id.llNewsItemContents));
	}
	
	protected abstract void buildEntryView(LayoutInflater objInflater, LinearLayout objRootLayout);
	@Override
	public abstract int hashCode();

	public void changeState() {
		setState(!this.boolIsOpen);
	}
	
	public void setState(boolean boolIsOpen) {
		this.boolIsOpen = boolIsOpen;
		if (this.boolIsOpen) {
			this.objRoot.findViewById(R.id.llNewsItemContents).setVisibility(View.VISIBLE);
			((ImageView)this.objRoot.findViewById(R.id.imgNewsItemArrow)).setImageResource(R.drawable.toggler_arrow_selected);
		}
		else {
			this.objRoot.findViewById(R.id.llNewsItemContents).setVisibility(View.GONE);
			((ImageView)this.objRoot.findViewById(R.id.imgNewsItemArrow)).setImageResource(R.drawable.toggler_arrow_unselected);
		}
	}

	public boolean isOpen() {
		return this.boolIsOpen;
	}
	
	private void setStateHandler(int intResourceID) {
		this.objRoot.findViewById(intResourceID).setOnClickListener(this); 
	}
	
	@Override
	public void onClick(View objView) {
		changeState();
	}
}
